home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8183 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: anvil.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: How are multidimensional array stored in memory?
  5. Date: 1 Mar 1996 12:20:10 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4h7m5qINNqbp@anvil.ugrad.cs.ubc.ca>
  8. References: <4h6tk6$1d2@mn5.swip.net>
  9. NNTP-Posting-Host: anvil.ugrad.cs.ubc.ca
  10.  
  11. In article <4h6tk6$1d2@mn5.swip.net>,
  12. Chris Rossall <chris.rossall@mailbox.swipnet.se> wrote:
  13. >Hello I was just wondering how multidimensional arrays would be stored
  14. >in memory.For example info[20][3].
  15. >would infor[0][1] follow info[0][0] or would info[1][0] follow it?
  16. >Perhaps this is compiler dependant?
  17.  
  18. It is not compiler dependent. The least significant portion of the address
  19. depends on the very last index. Thus, the elements of info are stored in memory
  20. in the order:
  21.  
  22.     info[0][0]
  23.     info[0][1]
  24.     info[0][2]
  25.     info[1][0]
  26.     .
  27.     .
  28.     .
  29.     info[19][2]
  30.  
  31.  
  32. This is easy to remember, since the least significant index is on the right
  33. side. Another way to remember is that for incomplete array types, only the
  34. leftmost index can be []. E.g. 
  35.  
  36. char *twodim[][3] = {
  37.     {  "Joe", "Smith", "programmer" },
  38.     {  "Mary", "Jones", "engineer" },
  39.     /* more triplets */
  40. };
  41.  
  42. Of course, the inner braces can be elided.
  43.  
  44. In this case, twodim is an array of unspecified size (determined by the number
  45. of initializers) of little three-membered arrays of char pointers.
  46.  
  47. Only one [] is ever allowed, must be the leftmost index, and can be present by
  48. itself.  
  49. -- 
  50.  
  51.